<?php
/**
 * IndexController - The default controller class
 * 
 * @author
 * @version 
 */

require_once 'Zend/Dojo/Data.php';
require_once 'Zend/Date.php';
require_once 'Events.php';
require_once 'Status.php';
require_once 'Zend/Dojo.php';
require_once 'Zend/Controller/Action.php';

/**
 * Enter description here...
 *
 */
class IndexController extends Zend_Controller_Action {
	
	/**
	 * @var Events
	 */
	private $events;
	
	public function init() {
		$this->events = new Events ( );
		$this->view->addHelperPath ( '../application/default/views/helpers', 'Zend_View_Helper' );
		Zend_Dojo::enableView ( $this->view );
	}
	
	/**
	 * The default action - show the home page
	 */
	public function indexAction() {
		$this->view->events = $this->events->fetchAll ();
	}
	
	public function dataAction() {
		$status = new Status ( );
		
		$records = $this->events->fetchAll ();
		$events = $records->toArray ();
		
		$fixed = array ();
		foreach ( $events as $record ) {
			$start = new Zend_Date ( $record ['start'], 'YYYY-MM-DD HH:MM:SS' );
			$end = new Zend_Date ( $record ['end'], 'YYYY-MM-DD HH:MM:SS' );
			
			if ($start != $end) {
				$record ['end'] = $end->toString ();
			} else {
				unset ( $record ['end'] );
			}
			$record ['start'] = $start->toString ();
			$record ['color'] = $status->colorOf ( $record ['status_id'] );
			$fixed [] = $record;
		}
		
		$data = array ('events' => $fixed );
		$this->_helper->json ( $data );
	}
	
	public function jsonAction() {
		$status = new Status ( );
		$data = new Zend_Dojo_Data ( );
		$data->setIdentifier ( 'id' );
		
		$records = $this->events->fetchAll ();
		foreach ( $records as $record ) {
			$item = $record->toArray ();
			$item ['status'] = $status->titleOf ( $record->status_id );
			$data->addItem ( $item );
		}
		$this->_helper->json ( $data->toArray () );
	}
	
	public function milestonesAction() {		
		$records = $this->events->getMilestones();
		$events = $records->toArray ();
		
		$fixed = array ();
		foreach ( $events as $record ) {
			$start = new Zend_Date ( $record ['start'], 'YYYY-MM-DD HH:MM:SS' );
			$record ['start'] = $start->toString ();
			$end = new Zend_Date ( $record ['end'], 'YYYY-MM-DD HH:MM:SS' );
			$record ['end'] = $end->toString ();
			$fixed [] = $record;
		}
		$this->_helper->json ( $fixed );		
	}
	
}
